package com.class5.springboot.controller;


import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.config.Audience;
import com.class5.springboot.entity.TbXtUser;
import com.class5.springboot.entity.vo.TbXtUserVo;
import com.class5.springboot.frame.web.SuperRestController;
import com.class5.springboot.frame.web.result.ResultData;
import com.class5.springboot.service.facade.ILoginService;
import com.class5.springboot.util.common.JwtHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController extends SuperRestController {

    @Autowired
    private Audience audience;

    @Autowired
    private ILoginService loginService;

    /**
     * @api {POST}  /class5/pub/login 01.登录
     * @apiGroup Login
     * @apiDescription  登录
     * @apiParamExample 参数说明
     * {
     * 	    userName:用户名,
     * 	    passWord:密码
     * }
     * @apiSuccessExample 成功后返回
     * {
     *     success:true,
     *     content:{
     *         token:登录凭证,
     *         userName:用户名,
     *         userRealName:用户真实姓名
     *     }
     *  }
     */
    @PostMapping("/pub/login")
    public ResultData login(HttpServletRequest req){
        JSONObject param = getBodyObject(req);
        String username = param.getString("userName");
        String password = param.getString("passWord");
//        String vcode = param.getString("vcode");
//        String vcodekey = param.getString("vcodekey");
        log.info("username:{}, password:{}, vcode:{}, vcodekey:{}", username, password);
//        checkNotEmpties(username,"帐号", password,"密码");
//        if(!codeBean.checkImageCode(vcodekey, vcode)) {
//            throw new AppException("验证码不正确");
//        }
            TbXtUserVo xtUser = loginService.login(username,password,getRealIp(req));
//            OrgInfo info = orgInfoService.getById(orgUser.getOrgId());
//            if(info.getStatus().equals(OrgInfoStatus.DISABLED)) {
//                throw new AppException("当前机构已被暂停使用");
//            }
//            JSONObject obj = new JSONObject();
//            if(info.getStatus().equals(OrgInfoStatus.NOTOPEN)) {
//                //api/auth/reg
//                obj.put("token", jwtService.generateForWeb(orgUser.getId(), info.getId(), true));
//                obj.put("auditStatus", info.getAuditStatus());
//                obj.put("cause", info.getAuditCause());
//                obj.put("orgId", info.getId());
//                obj.put("orgName", info.getOrgName());
//                obj.put("userId", orgUser.getId());
//                obj.put("userRealName", orgUser.getRealName());
//                obj.put("orgType",info.getOrgInfoTypeId());
//            }else if(info.getStatus().equals(OrgInfoStatus.EXPRESS)){
//                //TODO:资质或已超期
//                throw new AppException("当前机构资质已超期");
//            }else if(info.getStatus().equals(OrgInfoStatus.NORMAL)) {
//                obj.put("token", jwtService.generateForWeb(orgUser.getId(), info.getId(), false));
//                obj.put("auditStatus", info.getAuditStatus());
//                obj.put("cause", info.getAuditCause());
//                obj.put("orgId", info.getId());
//                obj.put("orgName", info.getOrgName());
//                obj.put("userId", orgUser.getId());
//                obj.put("userRealName", orgUser.getRealName());
//                obj.put("orgType",info.getOrgInfoTypeId());
//                //TODO:前期先直接进入企业端系统
//                obj.put("pageurl", "/aup2org/");
//                List<ProAppVo> paList = this.proAppService.getProApps(info.getId());
//                obj.put("appData",paList);
//            }

            String jwtToken = JwtHelper.createJWT(username,
                    password,
                    xtUser.getRoleId(),
                    audience.getClientId(),
                    audience.getName(),
                    audience.getExpiresSecond()*1000,
                    audience.getBase64Secret());
            JSONObject obj = new JSONObject();
            obj.put("token", jwtToken);
            obj.put("userName", xtUser.getUserName());
            obj.put("userRealName", xtUser.getRealName());
            return successWraper(obj);
    }

    /**
     * @api {POST}  /class5/pub/register 02.注册
     * @apiGroup Login
     * @apiDescription  注册
     * @apiParamExample 参数说明
     * {
     * 	    userName:用户名,
     * 	    passWord:密码,
     * 	    userRole:用户角色,
     * 	    userName:真实姓名,
     * 	    sex:性别  //0女   1男,
     * 	    email:邮箱,
     * 	    phone:电话,
     * @apiSuccessExample 成功后返回
     * {
     *     success:true,
     *     content:返回信息
     *  }
     */
    @PostMapping("/pub/register")
    public ResultData register(HttpServletRequest req){
        JSONObject param = getBodyObject(req);
        log.info("系统注册{},{}", param.toString());
        loginService.register(param);
        return successWraper(param);
    }

}
